const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


const QRCode = require('../../utils/weapp-qrcode.js');

import rpx2px from '../../utils/rpx2px.js';
let qrcode;
import { ycOrderdetail } from 'ycOrderdetail.model.js';

var time = require('../../utils/utils.js');

var ycorderdetail = new ycOrderdetail();
var oid = '';
var stime = '';
var state = '';
var sDataTime = '';
var state = '';
Page({
  data: {
    showIcon: true,
    //显示返回键以及首页
    clock: '',
    //倒计时渲染
    boolean: true,
    //订单未支付，未超时
    oid: '',
    stime: '',
    bukuanFlag: true,
    //待补款
    shenqingFlag: true,
    showMessage: false,
    CodeFlag: true,
    //二维码
    kefuphoneArr: []
  },
  onLoad: function (options) {
    oid = options.oid;
    this.data.oid = oid;

    this._loadDate();

    ycorderdetail.gettuikuan1(oid, res => {
      this.setData({
        res: res['state'],
        res1: res['owntk']
      });
    }); // 获取客服手机号

    let kefuphone = tt.getStorageSync('kefuphoneArr');

    if (!kefuphone) {
      util.kefuphone();
    }

    this.setData({
      kefuphoneArr: kefuphone
    });
  },
  tapHandler: function (e) {
    console.log(e);
    qrcode = new QRCode('canvas', {
      text: e.currentTarget.dataset.code,
      //转化为二维码的字符串
      width: 130,
      //长度
      height: 130,
      //宽度  
      colorDark: "#000000",
      //交替颜色
      colorLight: "#ffffff",
      //交替颜色
      correctLevel: QRCode.CorrectLevel.H //应该是准确度

    });
    console.log(e);
    qrcode.makeCode(e.currentTarget.dataset.code); //用元素对应的code更新二维码

    this.setData({
      CodeFlag: false
    });
  },
  CloseCode: function () {
    this.setData({
      CodeFlag: true
    });
  },
  _loadDate: function () {
    ycorderdetail.getQuery(this.data.oid, data => {
      console.log(data);

      if (data['damai_order_id']) {
        console.log(123);
        ycorderdetail.getdamai(data['damai_order_id'], res => {
          console.log(res);
          this.setData({
            damai_order: res
          });
        });
      }

      state = data['state'];
      stime = data.stime;
      sDataTime = data.sDataTime;
      var stimeDate = time.formatTimeTwo(stime, 'Y/m/d h:m:s');

      if (state == '订单完成') {
        //二维码接口
        ycorderdetail.getaid(oid, res => {
          console.log(res);

          if (res[0]['state'] == 2) {
            ycorderdetail.getewm(oid, res => {
              console.log(res);
              res = res[0]['smscrad'];
              this.setData({
                ewm: res
              });
            });
            var aid = res[0]['aid'];
            var smscrad = res[0]['smscrad'];
            console.log(aid);

            if (res[0]['verification'] != 2 && smscrad != '') {
              console.log(1);
              var a = 1;
            } else {
              var a = 0;
            }
          } else {
            var a = 0;
          }

          console.log(a);
          this.setData({
            state: a
          });
        });
      }

      var that = this;
      state = data.state;
      console.log(11, data.state);
      this.data.stime = stime;
      this.data.state = state;
      count_down(that);
      this.setData({
        'listfint': data,
        'stimeDate': stimeDate
      });
      ycorderdetail.getBuKuan(data.aid, res => {
        that.setData({
          'BuKuan': res
        });
      });
      var NowTime = new Date().getTime(); //获取系统当前时间戳

      var _time = sDataTime * 1000 - NowTime; //下单以后剩余的时间

      /* 分秒倒计时 */


      function count_down(that) {
        if (state == '未付款') {
          //在未付款的情况下
          if (_time > 0) {
            //订单未失效
            that.setData({
              clock: date_format(_time),
              // 渲染倒计时时钟
              timeFlag: false,
              state1Flag: false,
              state2Flag: true
            });
          } else {
            //订单已失效
            ycorderdetail.getStatus(oid, data => {
              that.onLoad();
            });
            that.setData({
              timeFlag: true,
              state1Flag: true,
              state2Flag: false
            });
          }
        }

        setTimeout(function () {
          // 倒计时效果
          _time -= 1000;
          count_down(that);
        }, 1000);
      }

      function date_format(micro_second) {
        var second = Math.floor(micro_second / 1000); // 秒数

        var hr = Math.floor(second / 3600); // 小时位

        var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60)); // 分钟位

        var sec = fill_zero_prefix(second - hr * 3600 - min * 60); // 秒位

        return "支付剩余时间：" + min + ":" + sec;
      } // 位数不足补零


      function fill_zero_prefix(num) {
        return num < 10 ? "0" + num : num;
      }
    });
    ycorderdetail.getUserInfo(this.data.oid, data => {
      this.setData({
        'UserInfo': data
      });
    });
  },
  QuXiaoClick: function () {
    tt.showModal({
      title: '提示',
      content: '确定取消此订单？',
      success: function (res) {
        if (res.confirm) {
          ycorderdetail.getStatus(oid, data => {});
          tt.switchTab({
            url: '/pages/index/index'
          });
        }
      }
    });
  },
  bukuanTap: function () {
    // 待补款说明
    this.setData({
      bukuanFlag: !this.data.bukuanFlag
    });
  },
  GouWu: function (e) {
    var id = e.target.dataset.aid; //console.log(id)

    tt.navigateTo({
      url: '../ycdetail/ycdetail?id=' + id //点击跳转到详情页面

    });
  },

  /*
   *开始支付
   * params:
   * id - {int}订单id
   */
  zhifuClick: function (e) {
    var that = this;
    var id = e.target.dataset.oid;
    ycorderdetail.execPay(id, statusCode => {
      if (statusCode != 0) {
        tt.showToast({
          title: '支付成功'
        }); //显示2秒隐藏加载提示

        setTimeout(function () {
          tt.hideToast();
        }, 2000);
        var flag = statusCode == 1;
        tt.navigateTo({
          url: '../ycOrder/ycOrder?flag=' + flag + '&id=' + id
        });
      } else {
        that._orderFail(statusCode); // 下单失败

      }
    });
  },
  _orderFail: function (statusCode) {
    tt.showModal({
      title: '下单失败',
      content: '请联系客服 400-800-7000',
      showCancel: false,
      success: function (res) {
        tt.navigateTo({
          url: '../ycOrder/ycOrder'
        });
      }
    });
  },
  //分享
  onShareAppMessage: function () {
    util.sharePage();
  },
  // 底部拨打电话
  calling: function () {
    util.calling();
  },
  calling2: function (e) {
    console.log(e);
    var tel = e.currentTarget.dataset.tel;
    tt.makePhoneCall({
      phoneNumber: tel
    });
  },
  //点击显示申请弹框
  shenqingSure: function () {
    this.setData({
      shenqingFlag: false
    });
  },
  //点击关闭申请弹框
  shenqingCancle: function () {
    this.setData({
      shenqingFlag: true
    });
  },
  showMessage: function (text) {
    var that = this;
    that.setData({
      showMessage: true,
      messageContent: text
    });
    setTimeout(function () {
      that.setData({
        showMessage: false,
        messageContent: ''
      });
    }, 3000);
  }
});